﻿

namespace Tentacle
{
    using System;
    using System.Diagnostics;
    using System.ComponentModel.DataAnnotations;
    using System.IO;
    using System.Collections.Generic;
    using System.Text;

    //
    using Tentacle.Processor;
    using Tentacle.Pipeline;
    using Tentacle.Core;

    //
    using HtmlAgilityPack;
    using SQLite;

    //
    using static System.Diagnostics.Debug;

    public sealed class Hjav5268Processor : IPageProcessor, IWebPipeline
    {
        [Table("Hjav5268")]
        public sealed class _Resources
        {
            [AutoIncrement]
            [PrimaryKey]
            public int Id { get; set; }

            /// <summary>
            /// 
            /// </summary>
            [NotNull]

            public string Title { get; set; }

            /// <summary>
            /// 
            /// </summary>
            [NotNull]
            public string Url { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string Thumbnail { get; set; }

            public override string ToString()
            {
                return $"[{Title}]<{Url},{Thumbnail}>";
            }
        }

        HtmlDocument _document;
        object[] _src;

        public Hjav5268Processor()
        {
            _document = new HtmlDocument();
        }

        #region IPageProcessor

        public bool IsPageMatch(WebPage page)
        {
            return true;
        }

        public WebSite[] Process(WebPage page)
        {           

            _document.LoadHtml(page.Html);
            var root = _document.DocumentNode;

            // link

            var node = root.SelectNodes(".//a");
            var site = new WebSite[node.Count];
            int i = 0;
            foreach (var n in node) {
                if (n.Attributes.Contains("href")) {
                    var href = n.Attributes["href"].Value;
                      if(href.Contains("porn68jav.com"))
                        site[i] = new WebSite(href);
                }
                i++;
            }

            // resource
            var src = root.SelectNodes("//div[contains(@class,'item cf')]");
            _src = new _Resources[src.Count];
            i = 0;
            foreach (var n in src) {
                var a = n.SelectSingleNode(".//div/a");
                try {
                    _src[i++] = new _Resources
                    {
                        Url = a.Attributes["href"].Value,
                        Title = a.Attributes["title"].Value,
                        Thumbnail = a.SelectSingleNode(".//span/img").Attributes["src"].Value
                    };
                }
                catch (Exception ex) {
                    WriteLine(ex);
                }
            }

            return site;
        }
        public object[] Resources {
            get {
                return _src;
            }
        }

        #endregion

        #region IWebPipeline

        public void Create(string file)
        {
            _connection = new SQLiteConnection(file);
            try {
                _connection.CreateTable<_Resources>();
            }
            catch {

            }
        }

        SQLiteConnection _connection;

        public void Store(object[] Resources)
        {
            foreach (_Resources src in Resources) {
                _connection.Insert(src);
            }
        }

        #endregion
    }
}
